<?php

class Date {
	private $day;
	private $month;
	private $year;

	private $hours;
	private $seconds;
	private $minutes;

	private $milliseconds;

	public function __construct($timestamp = null) {
		if ($timestamp == null) {
			if (function_exists('microtime'))
			$timestamp = microtime (true);
			else
			$timestamp = time();
		}
		
		$this->day = date ("d", $timestamp);
		$this->month = date ("m", $timestamp);
		$this->year = date ("Y", $timestamp);
		$this->hours = date ("H", $timestamp);
		$this->minutes = date ("i", $timestamp);
		$this->seconds = date ("s", $timestamp);
		$tmp = explode(".", $timestamp);
		if (count($tmp) <= 1) {
			$this->milliseconds = 0;
		} else {
			$this->milliseconds = $tmp[1];
		}
	}

	public function getDay() {
		return $this->day;
	}

	public function setDay($value) {
		$this->day = $value;
		return $this;
	}

	public function getMonth() {
		return $this->month;
	}

	public function setMonth($value) {
		$this->value = $value;
		return $this;
	}

	public function getYear() {
		return $this->year;
	}

	public function setYear($value) {
		$this->year = $value;
		return $this;
	}

	public function getHours() {
		return $this->hours;
	}

	public function setHours($value) {
		$this->hours = $value;
		return $this;
	}

	public function getMinutes() {
		return $this->minutes;
	}

	public function setMinutes($value) {
		$this->minutes = $value;
		return $this;
	}

	public function getSeconds() {
		return $this->seconds;
	}

	public function setSeconds($value) {
		$this->seconds = $value;
		return $this;
	}

	public function getMilliseconds() {
		return $this->milliseconds;
	}

	public function setMilliseconds($value) {
		$this->milliseconds = $value;
		return $this;
	}

	public function getTime() {
		return mktime($this->hours, $this->minutes, $this->seconds, $this->month, $this->day,  $this->year).".".$this->milliseconds;
	}

	public function __toString() {
		$timestamp = $this->getTime();
		return date ("Y-m-d H:i:s", $timestamp) . "." . $this->milliseconds;
	}
	
	public function format($fmt) {
		return date ($fmt, $this->getTime());
	}
	
	public function toMysqlDateString() {
		return date ("Y-m-d", $this->getTime());
	}
		
	public function toMysqlDateTimeString() {
		return date ("Y-m-d H:i:s", $this->getTime());
	}
			
	public function toMysqlTimeString() {
		return date ("H:i:s", $this->getTime());
	}
}
